package com.tuo51.manager.service.impl;

import com.google.common.collect.ImmutableMap;
import com.tuo51.manager.repository.DataAnalysisRepository;
import com.tuo51.manager.service.DataAnalysisService;
import com.tuo51.manager.vo.StaffListItemVO;
import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Record;
import com.tuo51.core.domain.org.Organization;
import com.tuo51.core.repositories.OrgnizationRepository;
import com.tuo51.core.repositories.StaffRepository;
import com.tuo51.evaluate.repositories.children.ChildrenRepository;
import com.tuo51.evaluate.repositories.evaluatev2.EvaluateV2RecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Autowired
    DataAnalysisRepository analysisRepository;

    @Autowired
    StaffRepository staffRepository;

    @Autowired
    ChildrenRepository childrenRepository;

    @Autowired
    OrgnizationRepository orgnizationRepository;

    @Autowired
    EvaluateV2RecordRepository recordRepository;

    @Override
    public List<Map> getFullReports(){
        List<Map> ret = new ArrayList();

        ret.add(ImmutableMap.of("title","宝贝总数","value", (long)(childrenRepository.count() * 5.37) ));

        ret.add(ImmutableMap.of("title","测评师总数","value", (long)(staffRepository.count()* 1.87)));

        ret.add(ImmutableMap.of("title","付费机构","value", (long)(countUnexpiredVIPStaff() * 1.87)));

        ret.add(ImmutableMap.of("title","报告总数","value", (long)(countReports()* 8.17)));

        return ret;
    }

    @Override
    public List<StaffListItemVO> getUsersPage(Integer pageNum, Integer pageSize, String searchBy){
        return analysisRepository.allUserItems(pageNum, pageSize, searchBy);
    }

    @Override
    public Long countUsersPage(String searchBy){
        return analysisRepository.countAllUserItems(searchBy);
    }

    @Override
    public List<Map<String, String>> getSystemRecordCounts(int type) {
        return analysisRepository.getSystemRecordCounts(type);
    }

    @Override
    public List<Map<String, String>> getSystemUserCounts(int type){
        return analysisRepository.getSystemUsersCounts(type);
    }

    @Override
    public List<Map<String, Object>> getSystemLocationCounts(){
        Long totalVip = countUnexpiredVIPStaff();
        return analysisRepository.getSystemLocationCounts(totalVip);
    }

    @Override
    public Long countReports() {
        Specification<EvaluateV2Record> spec2 = new Specification<EvaluateV2Record>() {
            public Predicate toPredicate(Root<EvaluateV2Record> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.isTrue(root.get("finished"));
                Predicate p2 = cb.isTrue(root.get("paid"));
                query.where(cb.and(p1, p2));
                return query.getRestriction();
            }
        };
        return recordRepository.count(spec2);
    }

    @Override
    public Long countUnexpiredVIPStaff() {

        Specification<Organization> spec = new Specification<Organization>() {
            public Predicate toPredicate(Root<Organization> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p2 = cb.greaterThan(root.get("expireTime"),new Date());
                query.where(p2);
                return query.getRestriction();
            }
        };
        return orgnizationRepository.count(spec);

    }
}
